<?php

/**
 * 退货操作
 */

namespace app\order\common;

class ReturnCommon extends \app\member\common\MemberCommon {


    protected function index() {
        $this->pageInfo['name'] = '退货管理';
        $this->setMeta('退货管理');
        $this->setCrumb([
            [
                'name' => '退货管理',
                'url' => url()
            ]
        ]);
        $type = request('get', 'type', 0 , 'intval');
        $urlParam['type'] = $type;
        $where = [];
        $where['A.user_id'] = $this->userInfo['user_id'];
        switch($type) {
            case 1:
                $where['A.status'] = 1;
                break;
            case 2:
                $where['A.status'] = 2;
                break;
            case 3:
                $where['A.status'] = 3;
                break;
            case 4:
                $where['A.status'] = 4;
                break;
            case 5:
                $where['A.status'] = 0;
                break;
        }
        $model = target('order/OrderReturn');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, 10, $urlParam);
        $list = $model->loadList($where, $pageData['limit'], 'order_id desc');

        $this->assign('pageList', $list);
        $this->assign('page', $pageData['html']);
        $this->assign('type', $type);
        $this->memberDisplay();
    }

    protected function info() {
        $returnNo = request('', 'return_no');
        $info = target('order/OrderReturn')->getWhereInfo([
            'return_no' => $returnNo
        ]);
        if (empty($info)) {
            $this->error('该退货单不存在!');
        }

        $orderInfo = target('order/Order')->getInfo($info['order_id']);

        if ($info['user_id'] <> $this->userInfo['user_id']) {
            $this->error404();
        }

        $this->pageInfo['name'] = '退货详情';
        $this->setMeta('退货详情');
        $this->setCrumb([
            [
                'name' => '退货详情',
                'url' => url()
            ]
        ]);
        $this->assign('info', $info);
        $this->assign('orderInfo', $orderInfo);
        $this->memberDisplay();
    }

    protected function saveDelivery() {
        $returnNo = request('', 'return_no');
        $info = target('order/OrderReturn')->getWhereInfo([
            'return_no' => $returnNo
        ]);
        if (empty($info)) {
            $this->error('该退货单不存在!');
        }
        if($info['status'] <> 2) {
            $this->error('该退货单无法保存!');
        }
        if ($info['user_id'] <> $this->userInfo['user_id']) {
            $this->error('无法保存该订单!');
        }
        if($info['delivery_name'] || $info['delivery_no'] ) {
            $this->error('您已经提交退货快递信息!');
        }
        $deliveryName = request('', 'delivery_name', '', 'html_clear');
        $deliveryNo = request('', 'delivery_no', '', 'html_clear');
        if(empty($deliveryName) || empty($deliveryNo)) {
            $this->error('快递信息未填写完整!');
        }
        $status = target('order/OrderReturn')->edit([
            'return_id' => $info['return_id'],
            'delivery_name' => $deliveryName,
            'delivery_no' => $deliveryNo
        ]);
        if (!$status) {
            target('order/OrderReturn')->rollBack();
            $this->error('退货信息保存失败!');
        }
        $this->success('退货信息保存成功!');
    }

    protected function cancel() {
        $returnNo = request('', 'return_no');
        $info = target('order/OrderReturn')->getWhereInfo([
            'return_no' => $returnNo
        ]);

        if (empty($info)) {
            $this->error('该退货单不存在!');
        }
        if($info['status'] <> 1 && $info['status'] <> 2) {
            $this->error('该退货申请无法取消!');
        }
        if ($info['user_id'] <> $this->userInfo['user_id']) {
            $this->error('无法取消该订单!');
        }

        target('order/OrderReturn')->beginTransaction();
        $status = target('order/OrderReturn')->edit([
            'return_id' => $info['return_id'],
            'status' => 0,
            'process_time' => time()
        ]);
        if (!$status) {
            target('order/OrderReturn')->rollBack();
            $this->error('申请取消失败,请稍后再试!');
        }

        $status = target('order/OrderGoods')->edit([
            'id' => $info['id'],
            'service_status' => 0
        ]);
        if (!$status) {
            target('order/OrderReturn')->rollBack();
            $this->error('申请取消失败,请稍后再试!');
        }
        target('order/OrderReturn')->commit();
        $this->success('退货申请取消成功!');

    }

    protected function push() {
        $id = request('', 'id');
        $info = target('order/OrderGoods')->getWhereInfo([
            'id' => $id
        ]);
        if (empty($info)) {
            $this->error('该订单商品不存在');
        }
        if ($info['service_status']) {
            $this->error('该商品正在售后状态!');
        }

        $target = target('order/Order');
        $orderInfo = $target->getInfo($info['order_id']);

        if ($orderInfo['order_user_id'] <> $this->userInfo['user_id']) {
            $this->error404();
        }

        if(!$orderInfo['order_status']) {
            $this->error('该订单已关闭!');
        }

        if($orderInfo['status_data']['action'] <> 'comment' && $orderInfo['status_data']['action'] <> 'complete') {
            $this->error('暂时无法进行退货操作!');
        }


        if (!isPost()) {
            $this->pageInfo['name'] = '商品退货';
            $this->setMeta('商品退货');
            $this->setCrumb([
                [
                    'name' => '商品退货',
                    'url' => url('', ['id' => $id])
                ]
            ]);

            $this->assign('id', $id);
            $this->assign('info', $info);
            $this->assign('orderInfo', $orderInfo);
            $this->assign('causeList', target('order/OrderReturn')->causeList());
            $this->memberDisplay();
        } else {
            $cause = str_len(request('post', 'cause', '', 'html_clear'), 250);
            $content = str_len(request('post', 'content', '', 'html_clear'), 300);
            $money = request('post', 'money', 0, 'price_format');

            if (empty($cause)) {
                $this->error('请选择退货原因!');
            }
            if (empty($content)) {
                $this->error('请填写退货详情!');
            }
            if(empty($money)) {
                $this->error('请填写退货金额!');
            }
            if($money > $info['price_total']) {
                $this->error('退货金额不能大于购买金额!');
            }
            $images = $_POST['images'];
            if (!empty($images) && is_array($images)) {
                $httpHost = 'http://' . trim($_SERVER["HTTP_HOST"], '/');
                foreach ($images as $image) {
                    if (substr($image, 0, 1) <> '/' && strpos($image, $httpHost, 0) === false) {
                        $this->error('您上传的图片有误,请重新上传!');
                    }
                }
            }else {
                $images = [];
            }
            $images = $images ? $images : [];

            target('order/OrderReturn')->beginTransaction();
            $status = target('order/OrderReturn')->add([
                'order_goods_id' => $info['id'],
                'user_id' => $this->userInfo['user_id'],
                'money' => $money,
                'cause' => $cause,
                'content' => $content,
                'images' => serialize($images),
                'status' => 1,
                'create_time' => time(),
                'return_no' => target('order/Order','service')->logNo()
            ]);
            if (!$status) {
                target('order/OrderReturn')->rollBack();
                $this->error('申请提交失败,请稍后再试!');
            }
            $status = target('order/OrderGoods')->edit([
                'id' => $id,
                'service_status' => 1
            ]);
            if (!$status) {
                target('order/OrderReturn')->rollBack();
                $this->error('申请提交失败,请稍后再试!');
            }
            target('order/OrderReturn')->commit();
            $this->success('退货申请提交成功,等待工作人员审核!', url('index'));
        }

    }


}